package com.dunqi.gpm.cangxi.home.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.cangxi.home.activity.CompleteEarlyInfoActivity;
import com.dunqi.gpm.cangxi.home.activity.EarlyAuditDetailActivity;
import com.dunqi.gpm.cangxi.home.activity.EditRequestActivity;
import com.dunqi.gpm.cangxi.home.adapter.EarlyInfoAdapter;
import com.dunqi.gpm.cangxi.home.bean.EarlyInfo;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/11/26.
 */

public class EarlyInfoFragment extends BaseFragment implements AdapterView.OnItemSelectedListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;

    List<EarlyInfo> data = new ArrayList();

    HashMap<String, String> parms = new HashMap<>();
    private Project project;

    private EarlyInfoAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    @SuppressLint("CheckResult")
    private void net() {
        long projectId = project == null ? null : project.getProjectId();
        parms.put(Constance.PROJECT_ID, String.valueOf(projectId));
        parms.put(Constance.PAGE, String.valueOf(page));

        //审核审批
        if (HomeFragment.homeMenuId == 9) {
            parms.put("stage_status", "1,2");
        }

        //申请
        if (HomeFragment.homeMenuId == 10) {
//            parms.put("stage_status", "1");
            parms.put("pro_annual_plan_id", String.valueOf(project.getProAnnualPlanId()));
//            parms.remove(Constance.PROJECT_ID);
        }

        ApiFactory.getInstance()
                .queryProjectPreStageList(parms)
                .compose(RxSchedulers.io_main())
                .subscribe(earlyInfoPage -> {
                    SwipeUtil.loadCompleted(swipeRefresh);
                    ArrayList<EarlyInfo> list = earlyInfoPage.getList();
                    if (page == 1) {
                        data.clear();
                    }
                    data.addAll(list);
                    page++;
                    adapter.notifyDataSetChanged();

                    if (earlyInfoPage.isHasNextPage()) {
                        adapter.loadMoreComplete();
                    } else {
                        adapter.loadMoreEnd();
                    }
                }, throwable -> adapter.loadMoreFail());
    }

    @Override
    protected void initUiAndListener() {
        spinner.setOnItemSelectedListener(this);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        adapter = new EarlyInfoAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_early_info;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        project = (Project) arguments.getSerializable(Constance.PROJECT);
    }

    public static EarlyInfoFragment newInstance(Project project) {
        EarlyInfoFragment fragment = new EarlyInfoFragment();
        Bundle bundle = new Bundle();
        if (project != null) {
            bundle.putSerializable(Constance.PROJECT, project);
        }
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //EarlyAuditDetailActivity
        if (resultCode == 400) {
            refreshData();
        }
        //AddEarlyActivity
        if (resultCode == 500) {
            refreshData();
        }
        //CompleteEarlyInfoActivity
        if (resultCode == 365) {
            refreshData();
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        refreshData();
    }

    private void refreshData() {
        page = 1;
        net();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    //    0 创建
//1 提交审批申请(实际启动)
//2 完成审批(实际完成)
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EarlyInfo item = data.get(position);

        EarlyAuditDetailActivity.start(getActivity(), item);

        if (true) {
            return;
        }

        int stageStatus = item.getStageStatus();

        int levelType = UserUtil.getLevelType();

        if (HomeFragment.homeMenuId == 11) {
            return;
        }

        if (levelType != 3) {
            return;
        }


        String stageName = item.getStageName();
        long proAnnualPlanPreStageId = item.getProAnnualPlanPreStageId();
        if (stageStatus == 0) {
            EditRequestActivity.start(getActivity(), proAnnualPlanPreStageId, stageName);
        }

        if (stageStatus == 1) {
            CompleteEarlyInfoActivity.start(getActivity(), proAnnualPlanPreStageId, stageName);
        }


    }
}
